/*
 * RtpHeader.cpp
 *
 *  Created on: Dec 2, 2011
 *      Author: shaohong
 */

#include "RtpHeader.h"

#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "BitUtils.h"

#include <string>
#include <iostream>
#include <sstream>

using namespace std;

namespace nmps {


string RtpHeader::toString() {
	stringstream oss;
	oss << "RTP header:" << endl << "version = " << version << "; payloadType = "
			<< payloadType << "; SSRC = " << hex << SSRC << endl;

	return oss.str();

}

RtpHeader::RtpHeader() {
	//let's do some initialization to our member variables

	version = 2;
	usePadding = false;
	hasExtension = false;	//always false in our case
	CSRC_Count = 0;	// always 0 in our case
	useMarker = 0;
	payloadType = PCMA; // PCMA:8, PCMU:0
	seqNum = rand() % 1000;
	timeStamp = 160;	//
	SSRC = rand();

	memset(packetizedForm, 0, sizeof(packetizedForm)); // without extension, RTP header is 12 bytes in the simplest case
}

RtpHeader::~RtpHeader() {
	// TODO Auto-generated destructor stub
}

int RtpHeader::getCsrcCount() const {
	return CSRC_Count;
}

PayloadType RtpHeader::getPayloadType() const {
	return payloadType;
}

unsigned int RtpHeader::getSeqNum() const {
	return seqNum;
}

void RtpHeader::incrementSeqNum() {
	seqNum++;
}

int RtpHeader::getHeaderLength() {
	return sizeof(packetizedForm);
}


const unsigned char * RtpHeader::getPacketizedForm(){
	// here let's serialize the RTP header fields
	Quartet quatet1;
	Quartet quatet2;
	Quartet quatet3;

	//write down the version
	BitUtils::setRtpHeaderValue(&quatet1, 0, 2, this->version);

	// write down the padding
	BitUtils::setRtpHeaderValue(&quatet1, 2, 1, this->isUsePadding()? 1:0);

	// write down the extension
	BitUtils::setRtpHeaderValue(&quatet1, 3, 1, this->isHasExtension()?  1:0);

	// write down the CSRC count
	BitUtils::setRtpHeaderValue(&quatet1, 4, 4, this->getCsrcCount());

	// Write down the Marker bit
	BitUtils::setRtpHeaderValue(&quatet1, 8, 1, this->isUseMarker()? 1:0 );

	// Write down the payload type
	BitUtils::setRtpHeaderValue(&quatet1, 9, 7, this->getPayloadType() );

	// Write down the sequence number
	BitUtils::setRtpHeaderValue(&quatet1, 16, 16, this->getSeqNum() );

	memcpy(packetizedForm, quatet1.bytes, 4);

	//Now starts the quartet 2
	// write down timestamp
	BitUtils::setRtpHeaderValue(&quatet2, 0, 32, this->getTimeStamp());

	memcpy(packetizedForm+4, quatet2.bytes, 4);

	//Now starts the quartet 3
	// write down SSRC identifier
	BitUtils::setRtpHeaderValue(&quatet3, 0, 32, this->getSsrc());

	memcpy(packetizedForm+8, quatet3.bytes, 4);

	return (unsigned char *)packetizedForm;
}


uint32_t RtpHeader::getSsrc() const {
	return SSRC;
}

uint32_t RtpHeader::getTimeStamp() const {
	return timeStamp;
}

int RtpHeader::getVersion() const {
	return version;
}

bool RtpHeader::isHasExtension() const {
	return hasExtension;
}

bool RtpHeader::isUseMarker() const {
	return useMarker;
}

bool RtpHeader::isUsePadding() const {
	return usePadding;
}

void RtpHeader::setCsrcCount(int CSRC_Count) {
	this->CSRC_Count = CSRC_Count;
}

void RtpHeader::setHasExtension(bool hasExtension) {
	this->hasExtension = hasExtension;
}

void RtpHeader::setPayloadType(PayloadType payloadType) {
	this->payloadType = payloadType;
}

void RtpHeader::setSsrc(unsigned long SSRC) {
	this->SSRC = SSRC;
}

void RtpHeader::setTimeStamp(unsigned long timeStamp) {
	this->timeStamp = timeStamp;
}

void RtpHeader::setUseMarker(bool useMarker) {
	this->useMarker = useMarker;
}

void RtpHeader::setUsePadding(bool usePadding) {
	this->usePadding = usePadding;
}

void RtpHeader::setVersion(int version) {
	this->version = version;
}

} /* namespace nmps */
